package com.wss.lsl.lucene.demo.shipin.analyzer;

import java.io.IOException;
import java.io.StringReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;

public class AnalyzerUtils {

	public static void displayToken(String str, Analyzer a) {
		try {
			TokenStream tStream = a.tokenStream("", new StringReader(str));
			CharTermAttribute cta = tStream
					.addAttribute(CharTermAttribute.class);
			while (tStream.incrementToken()) {
				System.out.print("[" + cta + "]");
			}
			System.out.println();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void displayAllTokenInfo(String str, Analyzer a) {
		try {
			TokenStream ts = a.tokenStream("", new StringReader(str));
			PositionIncrementAttribute pia = ts
					.addAttribute(PositionIncrementAttribute.class);
			OffsetAttribute oia = ts.addAttribute(OffsetAttribute.class);
			TypeAttribute ta = ts.addAttribute(TypeAttribute.class);
			CharTermAttribute cta = ts.addAttribute(CharTermAttribute.class);
			StringBuilder sb = new StringBuilder();
			while (ts.incrementToken()) {
				sb.append(pia.getPositionIncrement());
				sb.append(":");
				sb.append(cta);
				sb.append("[");
				sb.append(oia.startOffset());
				sb.append("-");
				sb.append(oia.endOffset());
				sb.append("]---->");
				sb.append(ta);
				sb.append("\n");
				System.out.print(sb.toString());
				sb.setLength(0);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
